
/* 提问评价切换 */
$(".projects_right .projects_right_nav li").each(function(i) {
	$(this).click(function() {
		$(".projects_right_nav li[class='projects_right_nav_clo']").removeClass();
		$(this).addClass("projects_right_nav_clo");
		$(".box_main").hide().eq(i).show();
	});
});
					
$("#qreg").click(function(){
	showReg();
});
$("#qlogin").click(function(){
	showLogin();
});

//未登录订阅
$("#subscribe1").click(function(){
	var ids = "";
	var that = this;
	var other = $("#other1").val();
	var email = $("#email1").val();
	
	$("#taglist1 a").each(function(){
		var className = $(this).parent("li").attr("class");
        if(className == "rss_list_clo"){
        	ids += $(this).attr("id")+"|"+$(this).attr("categoryID")+",";
        }
    });
	if(ids.length==0 && other.length==0){
		prompt("请选择您感兴趣的标签或填写您感兴趣的标签");
		return false;
	}
	
	if(!email.IsEmail()){
		prompt("请输入正确的邮箱地址");
		return false;
	}
	
	$.getJSON(path+"json/subscribe?randomNum="+Math.random(), {
		tagids : encodeURIComponent(ids),
		other : encodeURIComponent(other),
		email : email
	}, function(res) {
		if(res.code==0){
			$("#subscribe_info1").show();
			$(that).removeClass("btn").addClass("un_btn btn");
			$(that).attr("disabled",true);
		}
	})
	
});

//已登录订阅
$("#subscribe2").click(function(){
	var ids = "";
	var that = this;
	var other = $("#other2").val();
	var email = "";
	
	$("#taglist2 a").each(function(){
		var className = $(this).parent("li").attr("class");
        if(className == "rss_list_clo"){
        	ids += $(this).attr("id")+"|"+$(this).attr("categoryID")+",";
        }
    });
	if(ids.length==0 && other.length==0){
		prompt("请选择您感兴趣的标签或填写您感兴趣的标签");
		return false;
	}

	$.getJSON(path+"json/subscribe?randomNum="+Math.random(), {
		tagids : encodeURIComponent(ids),
		other : encodeURIComponent(other),
		email : email
	}, function(res) {
		if(res.code==0){
			$("#subscribe_info2").show();
			$(that).removeClass("btn").addClass("un_btn btn");
			$(that).attr("disabled",true);
		}
	})
});

//增加订购数量
//增加订购数量
$(".num").change(function(){
	var typeId = $(this).parent().parent().attr('type');

    var n=$(this).val();
    //console.log(n);return;

    //var limitNum = parseInt($("#limitNum").html());
    var price = parseFloat($(this).parent().prev().val());

    var num=parseInt(n);
    //if(num > limitNum ){return false;}
    //$("#num").val(num);
    var total = Math.round(num*price*100)/100;

    if(itemID==17){
        total = (price-10)*num+10;
    }

    var tmp = '';
    total = "" + total;
    if(total.indexOf('.') > 0) {
    	var j = total.indexOf('.');
    	var mm = total.substring(j+1);
    	tmp = mm.length > 1 ? "￥"+ total : "￥"+ total + "0";
    } else {
    	tmp = "￥"+total+".00";
    }

    $(this).parent().next().find('strong').html(tmp);
});

//减少订购数量
$(".buy_quantity_down").click(function(){
	var n=$("#num").val();
	var num=parseInt(n)-1;
	var price = parseFloat($("#price").html());
	if(num == 0 ){return false;}
	$("#num").val(num);
	var total = num*price;
	if(itemID==17){
		total = (price-10)*num+10;
	}
	$("#total").html(total);
});

//收藏项目
function collect(obj){
	if(isLogin){
		var itemID = $(obj).attr("ItemID");
		$.getJSON(path+"json/collect?randomNum="+Math.random(), {
			id : itemID
		}, function(res) {
			if(res.code==0){
				$(obj).html("<b></b>已收藏");
			}else{
				//prompt(res.msg);
				$(obj).html("<b></b>已收藏");
			}
			
		})
	}else{
		showLogin();
	}
}

//获取问题列表
function getFaqList(p,itemID){
	$.getJSON(path+"json/getfaqlist?randomNum="+Math.random(), {
		id : itemID,
		p : p,
		fun:"getFaqList"
	}, function(res) {
		html="";
		if(res){
			$.each(res.data,function(i,v){
				answerName ="";
				answer = "";
				if(v.Answer.length>0){
					answerName = v.AnswerUser.NickName;
					answer = v.Answer;
				}else{
					answer = "管理员还没有给<a href=\"javascript:;\" onclick=\"open_answer(this);\" class=\"zc_answer\" itemid=\""+itemID+"\" qid=\""+v.ID+"\">回复</a>";
				}
                html +='<dl class="ask_list">';
                html +='<dt>'+v.User.NickName+'<em>'+v.CreateTime+'</em></dt>';
                html +=' <dd class="ask_question bk">'+ v.Question+'</dd>';
                html+='<dd class="ask_answer">';
                html+='<div class="ask_answer_info bk">'+answer+'</div>';
                html+='<p class="ask_answer_author">'+answerName+'</p>';
                html+='<span class="ask_answer_arrow"></span>';
                html +=' </dd>';
                html +='</dl>';
			});
			html+=res.page;
		}
		
		$("#faq_list").html(html);
	})
}

function getUserList(p,itemID){
    $.getJSON(path+"json/getuserlist?randomNum="+Math.random(), {
        id : itemID,
        p : p,
        fun:"getUserList"
    }, function(res) {
        html='<ul class="favorer_list">';
        if(res){
            $.each(res.data,function(i,v){
                html +='<li><a href="http://www.baidu.com/'+ v.UserID+'" target="_blank"><img src="'+ v.Photo+'" width="50" height="50" alt="'+ v.NickName+'" />'+ v.NickName+'</a></li>';
            });
            html +='</ul>';
            html+=res.page;
        }

        $("#listUser").html(html);
    })
}

//检查用户是否有回复问题权限
function open_answer(obj){
	
		$.getJSON(path+"json/CheckAnswerAuth?randomNum="+Math.random(), {
			itemid : itemID
		}, function(res) {
			if(res.code==0){
				$("#id_answer").val($(obj).attr("qid"));
				$("#itemid_answer").val($(obj).attr("itemid"));
				$.blockUI({ message: $('#answerForm') }); 
			}else{
				prompt(res.msg);
			}
		});
	
}

$("#do_cancel").click(function(){
	$.unblockUI();
});

//项目管理员回复
$("#do_answer").click(function(){
	var content = $("#content_answer").val();
	
	if(content.length==0){
		prompt("回复不能为空");
		return false;
	}
	if(content.CountCharacters()>500){
		prompt("回答内容长度超过最长限制！");
		return false;
	}
	var id=$("#id_answer").val();
	var itemid=$("#itemid_answer").val();
	$.post(path+"json/answerfaq", {
		content : content,
		id:id,
		itemid:itemid
	}, function(res) {
		if(res.code==0){
			getFaqList(1,itemID);
		}else if(res.code==1){
			showLogin();
		}else{
			prompt(res.msg);
		}
		$.unblockUI();
	},"json")
	
})


//获取评价列表
function getScoreList(p,itemID){
	$.getJSON(path+"json/getscorelist", {
		id : itemID,
		p : p,
		fun:"getScoreList"
	}, function(res) {
		html="";
		if(res){
			$.each(res.data,function(i,v){
				html +='<dl class="ask_list">';
                html +=' <dt>'+ v.NickName+'<em>'+ v.CreateTime+'</em></dt>';
                html +='  <dd class="ask_question bk">';
                html +='   '+ v.Memo+'';
                html +='       <div class="ask_question_stars">';
                html +='           <div class="pro_rating_action">';
                html +='               <em>评分：</em>';
                html +='                <div class="evaluate_score"><span class="info_stars" style="width:'+(v.Score/5)*100+'px"></span></div> ';
                html +='             </p>';
                html +='          </div>';
                html +='      </div>';
                html +='    </dd>';
                html +=' </dl>';
			});
			
			html+=res.page;
		}
		
		$("#score_list").html(html);
	})
}

//发表问题
$("#doit").click(function(){
	var content = $.trim($("#content_ask").val());
	if(content.length==0){
		prompt("请输入您要咨询的问题，谢谢！");
		return false;
	}
	if(content.CountCharacters()>100){
		prompt("问题长度超过最长限制！");
		return false;
	}
	$.post(path+"json/createfaq?randomNum="+Math.random(), {
		content : content,
		id:itemID,
		pid:pid
	}, function(res) {
		if(res.code==0){
            $("#content_ask").val("");
			getFaqList(1,itemID);
		}else if(res.code==1){
			showLogin();
		}else{
			prompt(res.msg);
		}
	},"json")
});

//评价
$("#review").click(function(){
	var content = $("#content_review").val();
	var score = $("#score_review").val();
	if(content.length==0){
		prompt("评价内容不能为空！");
		return false;
	}
	if(content.CountCharacters()>100){
		prompt("评价内容长度超过最长限制！");
		return false;
	}
	$.post(path+"json/createscore?randomNum="+Math.random(), {
		content : content,
		id:itemID,
		score:score
	}, function(res) {
		if(res.code==0){
			getItemScore(itemID);
			getScoreList(1,itemID);
		}else if(res.code==1){
			showLogin();
		}else{
			prompt(res.msg);
		}
	},"json")
});

function useful(obj){
	var id = $(obj).attr("reviewid");
	reviewGoodBad(1,id);
}

function useless(obj){
	var id = $(obj).attr("reviewid");
	reviewGoodBad(0,id);
}


function reviewGoodBad(t,id){
	$.getJSON(path+"json/ReviewGoodBad?randomNum="+Math.random(), {
		t : t,
		id:id
	}, function(res) {
		if(res.code==0){
			if(t==1){
				$("#useful").html(res.msg);
			}else{
				$("#useless").html(res.msg);
			}
		}
	})
}

//获取评分
function getItemScore(itemid){
	$.getJSON(path+"json/getItemScore?randomNum="+Math.random(), {
		id : itemID
	}, function(res) {
		if(res.code==0){
			var score = Number(res.msg);
			$("#score_avg").html(score);
			$("#star_width").width((score/5)*100);
		}
	})
}

$(".buy").click(function(){
	var num = $(this).parent().prev().prev().find('.num').val().Trim();
	var typeId = $(this).parent().parent().attr('type');

	$.getJSON(path+"json/checkPayItem?randomNum="+Math.random(), {
		itemid : itemID,
		n:num,
		typeId: typeId
	}, function(res) {
		if(res.code==0){
			location.href= "pay?itemid="+itemID+"&num="+num+ "&typeId=" + typeId;
		}else if(res.code==1){
			showLogin();
		}else{
			$.ksPlugin.faild(res.msg);
		}
	})
});

